package face.pay.open.config;

import com.face.pay.AliPayConfig;
import com.face.pay.IPayConfigServer;
import com.face.pay.SecurityConfig;
import com.face.pay.WxApiConfig;
import face.pay.dao.FileInfoDao;
import face.pay.dao.OemConfigInfoDao;
import face.pay.dao.OemInfoDao;
import face.pay.entity.FileInfo;
import face.pay.entity.OemConfigInfo;
import face.pay.entity.OemInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Auther: zkj
 * @Date: 2019/8/26 16:11
 * @Description:
 */
@Service
public class ApiConfigServer implements IPayConfigServer {

    @Autowired
    private OemInfoDao oemInfoDao;

    @Autowired
    private OemConfigInfoDao oemConfigInfoDao;

    @Autowired
    private FileInfoDao fileInfoDao;


    @Transactional
    public WxApiConfig getConfig(Integer oemId){

        OemInfo oemInfo = this.oemInfoDao.getOemInfoById(oemId);

        Long piclientCertp12 =oemInfo.getApiclientCertp12();


        FileInfo crtp12fileInfo = this.fileInfoDao.getById(piclientCertp12);

        WxApiConfig wxApiConfig =  new WxApiConfig();


        if(crtp12fileInfo!=null){

            OemConfigInfo oemConfigInfo =  this.oemConfigInfoDao.getByOemId(oemId);
            if(oemConfigInfo!=null){

                SecurityConfig securityConfig =  new SecurityConfig(
                        oemId,
                        oemConfigInfo.getAccessKeyId(),
                        oemConfigInfo.getAccessKeySecret(),
                        oemConfigInfo.getBucket(),
                        oemConfigInfo.getEndPoint(),
                        oemInfo.getAppId(),
                        oemInfo.getApiKey(),
                        oemInfo.getMchId(),
                        crtp12fileInfo.getFullName(),
                        oemInfo.getApi3Key(),
                        oemConfigInfo.getFaceKey()
                        );
                wxApiConfig.setSecurityConfig(securityConfig);

            }

        }

        return  wxApiConfig;

    }

    @Override
    public AliPayConfig getAliConfig(Integer oemId) {
        return null;
    }
}
